package com.imay.live.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.activity.profit.IncomeActivity;
import com.imay.live.R;

/**
 * Created by Administrator on 2017/04/28 0028.
 */

public class EditTextView extends LinearLayout {
    private Paint paint;
    private Path path;
    private int tag = 0;
    private EditText textphone;

    public EditTextView(Context context, int tag) {
        super(context);
        this.tag = tag;
        init(context);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (path == null) {
            path = new Path();
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(4);
            paint.setColor(getResources().getColor(R.color.divider_f0));
        }
        path.reset();
        path.moveTo(0, canvas.getHeight());
        path.lineTo(canvas.getWidth(), canvas.getHeight());
        canvas.drawPath(path, paint);
    }


    private void init(Context context) {
        setOrientation(HORIZONTAL);
        setBackgroundColor(getResources().getColor(R.color.bg_window));
        TextView texttitle = new TextView(context);
        texttitle.setTextColor(getResources().getColor(R.color.text_00));
        LayoutParams titleparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleparams.leftMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        titleparams.rightMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        titleparams.gravity = Gravity.CENTER_VERTICAL;
        addView(texttitle, titleparams);
        LayoutInflater.from(context).inflate(R.layout.editview,this,true);
        textphone = (EditText) findViewById(R.id.edit);
        textphone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                textphone.setFocusable(true);
                textphone.setFocusableInTouchMode(true);
                textphone.requestFocus();
                InputMethodManager inputManager =
                        (InputMethodManager) textphone.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(textphone, 0);
            }
        });
        textphone.setFocusable(false);
        textphone.setHintTextColor(getResources().getColor(R.color.text_b2));
        textphone.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.hint_size));
        textphone.setBackgroundColor(Color.TRANSPARENT);

//            textphone.setKeyListener(DigitsKeyListener.getInstance("1234567890"));
        LayoutParams tipparams = (LayoutParams) textphone.getLayoutParams();
        tipparams.gravity = Gravity.CENTER_VERTICAL;
        tipparams.rightMargin = (int) getResources().getDimension(R.dimen.profitreallyname_top);
        if (tag == 0) {
            texttitle.setText(getResources().getString(R.string.profitgetNo));
            textphone.setInputType(InputType.TYPE_CLASS_TEXT);
            textphone.setHint(getResources().getString(R.string.profithintNo));
            textphone.setImeOptions(EditorInfo.IME_ACTION_NEXT);
            textphone.setGravity(Gravity.CENTER_VERTICAL);
            textphone.setSingleLine();
        } else if (tag == 1) {
            texttitle.setText(getResources().getString(R.string.profitgetMeili));
            textphone.setHint(getResources().getString(R.string.profithintmeili, IncomeActivity.MINMEILI));
            textphone.setMaxLines(1);
            textphone.setImeOptions(EditorInfo.IME_ACTION_DONE);
            textphone.setInputType(InputType.TYPE_CLASS_NUMBER);
            textphone.setGravity(Gravity.CENTER_VERTICAL);
            textphone.setSingleLine();
        } else if (tag == 2) {
            tipparams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            textphone.setLayoutParams(tipparams);
            texttitle.setText(getResources().getString(R.string.vsign_des));
            textphone.setGravity(Gravity.RIGHT);
            textphone.setImeOptions(EditorInfo.IME_ACTION_DONE);
            textphone.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
            textphone.setHint(getResources().getString(R.string.vsign_deshint));
            setMinimumHeight((int) getResources().getDimension(R.dimen.profitreally_height));
        }
    }

    public void setTextphone(CharSequence text) {
        textphone.setText(text);
        textphone.setSelection(text.length());
    }

    public void setOnEditorActionListener(TextView.OnEditorActionListener l) {
        textphone.setOnEditorActionListener(l);
    }

    public void addTextChangedListener(TextWatcher watcher) {
        textphone.addTextChangedListener(watcher);
    }

    public void setFocusable() {
        textphone.setFocusable(true);
        textphone.setFocusableInTouchMode(true);
        textphone.requestFocus();
    }

    public String getTextString() {
        return textphone.getText().toString();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (tag == 2) {
            super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) getResources().getDimension(R.dimen.profitreally_height), MeasureSpec.AT_MOST));
        } else {
            super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) getResources().getDimension(R.dimen.profitreally_height), MeasureSpec.EXACTLY));
        }
    }
}
